<?php

class SparringbuyController extends \Base\AbstractController {

    /**
     * @desc 看大腿列表
     */
    public function listAction() {
        $request      = $this->getRequest();
        $class        = (int)$request->get('class');
        $keyword      = trim((string)$request->get('keyword'));
        $memberMapper = \Mapper\MemberModel::getInstance();
        $gameMapper   = \Mapper\SparringGameModel::getInstance();
        $detailMapper = \Mapper\SparringBuyDetailModel::getInstance();
        $where        = [];

        if(!empty($class) && !empty($keyword)) {
            switch($class) {
                case 1 :
                    $where = ['uid' => $keyword];
                    break;
                case 2 :
                    $memberModel = $memberMapper->fetch(['nick_name LIKE "%' . urlencode($keyword) . '%"']);
                    if($memberModel instanceof MemberModel) {
                        $where['uid'] = $memberModel->getId();
                    }
                    break;
                case 3 :
                    $memberModel = $memberMapper->fetch(['mobile' => $keyword]);
                    if($memberModel instanceof MemberModel) {
                        $where['uid'] = $memberModel->getId();
                    }
                    break;
            }

        }

        $page = new \Lib\Paginator('sparringBuys');
        $page->where($where);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();
        $return = [];

        foreach($models as $model) {
            $data = $model->toArray();
            $nickName = $memberMapper->findById($data['uid']);

            if($nickName instanceof \MemberModel) {
                $data['member_nick_name'] = urldecode($nickName->getNick_name());
            }

            $detailModels = $detailMapper->fetchAll(['sparring_buy_id' => $data['id']]);

            foreach($detailModels as $detailModel) {
                $detailData          = $detailModel->toArray();
                $data['game_name']   = $gameMapper->findById($detailData['game_id'])->getGame_name();
                $data['net_bar']     = $detailData['net_bar'];
                $data['price']       = $detailData['price'];
                $data['game_server'] = $detailData['game_server'];

                if($detailData['status'] == 2) {
                    $data['status'] = 3;
                    break;
                }
            }

            $return[] = $data;
        }

        $this->assign('lists', $return);
    }

    /**
     * @desc 订单详情
     */
    public function detailAction() {
        $request = $this->getRequest();
        $id      = $request->get('id');

        $page = new \Lib\Paginator('sparringBuyDetail');
        $page->where(['sparring_buy_id' => $id]);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();

        $gameMapper = \Mapper\SparringGameModel::getInstance();


        foreach($models as $model) {
            $data              = $model->toArray();
            $data['game_name'] = $gameMapper->findById($data['game_id'])->getGame_name();

            $return[] = $data;
        }

        $this->assign('lists', $return);
        $this->assign('title', $id);


    }

    /**
     * @desc 增
     */
    public function addAction() {

    }

    /**
     * @desc 删
     */
    public function delAction() {
        $request           = $this->getRequest();
        $id                = $request->get('id');
        $sparringBuyMapper = \Mapper\SparringBuysModel::getInstance();
        $detailBuyMapper   = \Mapper\SparringBuyDetailModel::getInstance();

        $sparringBuyModel = $sparringBuyMapper->findById($id);

        if($sparringBuyModel instanceof SparringBuysModel) {
            $buyStatus     = $sparringBuyModel->getStatus();
            $end           = $sparringBuyModel->getEnd_time();
            $sparringBuyId = $sparringBuyModel->getId();

            //状态正常且没有过期才能屏蔽
            if($buyStatus == \Lib\Consts::SPARRING_BUY_STATUS_NORMAL && $end > time()) {
                //开启事务
                $sparringBuyMapper->begin();

                $rows = $sparringBuyMapper->update(['status' => \Lib\Consts::SPARRING_BUY_STATUS_DEL], [
                    'id'     => $id,
                    'status' => \Lib\Consts::SPARRING_BUY_STATUS_NORMAL,
                ]);

                if($rows <= 0) {
                    $sparringBuyMapper->rollback();
                    return \Response::errmsg('屏蔽失败！');
                }

                $rows = $detailBuyMapper->update(
                    ['status' => \Lib\Consts::SPARRING_DETAIL_STATUS_DEL],
                    [
                        'sparring_buy_id' => $sparringBuyId,
                        'status'          => \Lib\Consts::SPARRING_DETAIL_STATUS_NORMAL,
                    ]);

                if($rows <= 0) {
                    $sparringBuyMapper->rollback();
                    return \Response::errmsg('屏蔽失败！');
                }

                $sparringBuyMapper->commit();
                return \Response::msg('屏蔽成功！');

            } else {
                return \Response::msg('订单已经过期！');
            }

        } else {
            return \Response::errmsg('系统错误！');
        }

    }

    /**
     * @desc 详情
     */
    public function editAction() {

    }

}
